package com.logistics.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.logistics.AdminUser;
import com.logistics.admin.BaseAction;
import com.logistics.service.AdminUserService;

@Component("loginAdminAction")
@Scope("prototype")
public class LoginAdminAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8186306803757950616L;

	/**
	 * 
	 */
	public LoginAdminAction() {
		super(false);
		setRequestMethodSensitive(true);
	}
	
	private AdminUserService userService;
	
	@Autowired
	public void setUserService(AdminUserService userService) {
		this.userService = userService;
	}

	private String username;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	private String password;

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	private boolean invalidUser;
	
	public boolean isInvalidUser() {
		return invalidUser;
	}

	public void setInvalidUser(boolean invalidUser) {
		this.invalidUser = invalidUser;
	}

	@Override
	protected String doGet() {
		return INPUT;
	}
	
	@Override
	protected String doPost() {
		
		AdminUser user = new AdminUser();
		user.setUsername(username);
		user.setPassword(password);
		
		try {
			userService.authenticate(user);
			
			user = userService.getByUsername(username);
			
			setLogonUser(user);
		} catch (Exception e) {
			invalidUser = true;
			return INPUT;
		}
		
		return SUCCESS;
	}
}
